package com.taomeng.servlet.admin;

import com.taomeng.constant.MessageConstant;
import com.taomeng.entity.PageBean;
import com.taomeng.entity.RespBody;
import com.taomeng.entity.User;
import com.taomeng.entity.UserInfo;
import com.taomeng.service.UserService;
import com.taomeng.servlet.BaseServlet;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/user/*")
public class UserServlet extends BaseServlet {
    @Autowired
    private UserService userService;

    public void add(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        Map<String, String[]> map = req.getParameterMap();
        String[] roleId = map.get("roleId");

        User user = new User();
        BeanUtils.populate(user, map);

        RespBody respBody = new RespBody();
        if (userService.add(user, Integer.parseInt(roleId[0]))) {
            respBody.setCode(200);
            respBody.setMsg("插入成功");
        } else {
            respBody.setCode(500);
            respBody.setMsg("插入失败");
        }
        RespBody.JsonObject(resp, respBody);

    }

    public void findAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int page = Integer.parseInt(req.getParameter("page"));
        int limit = Integer.parseInt(req.getParameter("limit"));

        PageBean<UserInfo> pageBean = userService.findByPage(page, limit);

        RespBody respBody = new RespBody();
        if (pageBean != null) {
            respBody.setCode(0);
            respBody.setMsg(MessageConstant.QUERY_SUCCESSFUL);
            respBody.setCount(pageBean.getTotal());
            respBody.setData(pageBean.getList());
        } else {
            respBody.setCode(500);
            respBody.setMsg(MessageConstant.QUERY_FAILS);
        }

        RespBody.JsonObject(resp, respBody);
    }

    public void del(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Long id = Long.parseLong(req.getParameter("id"));

        RespBody respBody = new RespBody();

        if (userService.delById(id)) {
            respBody.setCode(200);
            respBody.setMsg("删除成功");
        } else {
            respBody.setCode(500);
            respBody.setMsg("删除失败");
        }
        RespBody.JsonObject(resp, respBody);
    }

    public void update(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        Map<String, String[]> map = req.getParameterMap();
        String[] roleIds = map.get("roleId");
        User user = new User();
        BeanUtils.populate(user,map);

        RespBody respBody = new RespBody();
        if (userService.update(user,Integer.parseInt(roleIds[0]))) {
            respBody.setCode(200);
            respBody.setMsg("更新成功");
        }else {
            respBody.setCode(500);
            respBody.setMsg("更新失败");
        }

        RespBody.JsonObject(resp,respBody);
    }

    public void getUsers(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int deptId = Integer.parseInt(req.getParameter("deptId"));
        List<User> userList = userService.getUsersByDeptId(deptId);

        RespBody respBody = new RespBody();
        respBody.setCode(200);
        respBody.setMsg("查询成功");
        respBody.setCount((long) userList.size());
        respBody.setData(userList);

        RespBody.JsonObject(resp,respBody);
    }

    @Override
    public void init() throws ServletException {
        super.init();
        ServletContext application = this.getServletContext();
        // 解决servlet用@Autowired自动注入service失败的问题
        SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, application);
    }
}
